package com.example.tnot.Teacher.Messagepart.adpter;

import static com.example.tnot.Teacher.CoursePart.adapter.rvMyTeachAdapter.BASE_IMAGE_URL;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.tnot.R;
import com.example.tnot.Teacher.Messagepart.ItemBean.MessageHomeItemList;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MessageHomeAdapter extends RecyclerView.Adapter<MessageHomeAdapter.ViewHolder> {

    private List<MessageHomeItemList> MessageData;
    private int ItemClick;

    public MessageHomeAdapter(List<MessageHomeItemList> MessageData) {
        this.MessageData = MessageData;
    }

    @NonNull
    @Override
    public MessageHomeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_rv_message_home, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageHomeAdapter.ViewHolder holder, int position) {
        holder.UserMessage.setText(MessageData.get(position).getUserMessage());
        holder.UserName.setText(MessageData.get(position).getUserName());

        String courseId =String.valueOf(MessageData.get(position).getUserImage());
        String imageUrl = BASE_IMAGE_URL + courseId;

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.example_face)
                .error(R.drawable.example_face)
                .transform(new CenterCrop(), new RoundedCorners(8));

        Glide.with(holder.UserImage.getContext())
                .load(imageUrl)
                .apply(options)
                .into(holder.UserImage);

       // holder.UserImage.setImageResource(MessageData.get(position).getUserImage());

        //获取系统时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String time = simpleDateFormat.format(date);
        holder.UserTime.setText(time);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ItemClick = holder.getBindingAdapterPosition();
                NavController navController = Navigation.findNavController(v);
                Bundle bundle = new Bundle();
                bundle.putString("UserName", MessageData.get(ItemClick).getUserName());
                navController.navigate(R.id.action_messageContainerFragment_to_messageDetailFragment,bundle);
            }
        });
    }


    @Override
    public int getItemCount() {
        return MessageData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView UserImage;
        TextView UserName, UserMessage, UserTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            UserImage = itemView.findViewById(R.id.iv_UserImage);
            UserName = itemView.findViewById(R.id.tv_UserName);
            UserMessage = itemView.findViewById(R.id.tv_UserMessage);
            UserTime = itemView.findViewById(R.id.tv_time);
        }
    }


}
